/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.fourmob.datetimepicker.util;

import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;

/**
 * 用于存储
 *
 * @since 2021-04-09
 */
public class DateTimePickerR {
    static DateTimePickerR mPerfence;
    private static String PREFERENCE_FILE_NAME = "fileName";

    /**
     * 实例化
     *
     * @return DateTimePickerR
     */
    public static DateTimePickerR getInstance() {
        if (mPerfence == null) {
            mPerfence = new DateTimePickerR();
        }
        return mPerfence;
    }

    /**
     * 获取引用数据库支持
     *
     * @param context
     * @return Preferences
     */

    Preferences getApplicationPref(Context context) {
        DatabaseHelper databaseHelper = new DatabaseHelper(context);
        return databaseHelper.getPreferences(PREFERENCE_FILE_NAME);
    }

    public void putInt(Context context, String key, int value) {
        mPerfence.getApplicationPref(context).putInt(key, value).flushSync();
    }

    public int getInt(Context context, String key, int defvalue) {
        return mPerfence.getApplicationPref(context).getInt(key, defvalue);
    }

    /**
     * 设置布尔值
     *
     * @param context
     * @param key
     * @param value
     */

    public void putBoolean(Context context, String key, boolean value) {
        mPerfence.getApplicationPref(context).putBoolean(key, value).flushSync();
    }

    /**
     * 获取 布尔值
     *
     * @param context
     * @param key
     * @param defValue
     * @return boolean类型数据
     */

    public boolean getBoolean(Context context, String key, boolean defValue) {
        return mPerfence.getApplicationPref(context).getBoolean(key, defValue);
    }

    /**
     * 设置字符串值
     *
     * @param context
     * @param key
     * @param value
     */
    public void putString(Context context, String key, String value) {
        mPerfence.getApplicationPref(context).putString(key, value).flushSync();
    }

    /**
     * 获取字符串值
     *
     * @param context 获取上下文
     * @param key
     * @param defValue 默认值（当获取的数据为null时，设置这个默认值）
     * @return 字符串类型
     */
    public String getString(Context context, String key, String defValue) {
        return mPerfence.getApplicationPref(context).getString(key, defValue);
    }
}
